Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
Estilos CSS únicos para "+" y "-" cuando el usuario ingresa

Cuando el usuario escribe + en mi input , me gustaría que el + sea verde

Ej: +10000 --> "+" debe ser verde y 10000 debe ser negro

Cuando el usuario escribe - en mi input , me gustaría que - sea rojo

Ej: -10000 --> "-" debe ser rojo y 10000 debe ser negro

Mi idea era usar ::first-letter , pero me doy cuenta de que no funciona en input

¿Es esto posible con css y javascript? ¿Necesito algún Regex elegante para lograr esto?

 input { font-size: 100px; } /* only - should be red */ input::first-letter { color: red; } /* only + should be green */ input::first-letter { color: green; }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <link rel="stylesheet" href="styles.css" /> <title>Static Template</title> </head> <body> <input type="text" /> </body> </html>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Primero obtenga el elemento <input> con .getElementsByTagName('input')[0] , luego puede adjuntar un detector de eventos en keyup . Desde aquí, puede usar .style.color para actualizar el color según .value[0] :

 const target = document.getElementsByTagName('input')[0]; target.addEventListener('keyup', function() { if (this.value[0] === '-') { this.style.color = 'red'; } else if (this.value[0] === '+') { this.style.color = 'green'; } else { this.style.color = 'black'; } })
 input { font-size: 100px; }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <link rel="stylesheet" href="styles.css" /> <title>Static Template</title> </head> <body> <input type="text" /> </body> </html>

Tenga en cuenta que el fragmento anterior solo verifica el primer carácter ingresado. Si desea verificar si hay alguna aparición del carácter de destino, puede recorrer el .value .

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda